package com.itboye.lottery.adapter.base;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.activity.football.FootballMainActivity;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.FootballMatchBean;
import com.itboye.lottery.bean.ZqBetListItem;
import com.itboye.lottery.dialog.JczqHHDialog;
import com.itboye.lottery.enums.LotteryType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import static com.itboye.lottery.utils.BetSelectMatches.hashSet;
import static com.itboye.lottery.utils.BetSelectMatches.map;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private final Activity activity;
    private ArrayList<FootballMatchBean> footballMatchBeanArrayList;
    private int pager = 0;
    //    HashMap<String, Map<String, String>> map = new LinkedHashMap<>();
    private int matchCount;
    private ArrayList<ZqBetListItem> zqBetListItemArrayList = new ArrayList<>();

    public ArrayList<ZqBetListItem> getZqBetListItemArrayList() {
        return zqBetListItemArrayList;
    }

    public void setZqBetListItemArrayList(ArrayList<ZqBetListItem> zqBetListItemArrayList) {
        this.zqBetListItemArrayList = zqBetListItemArrayList;
    }

    public int getMatchCount() {
        Log.v("", map.toString());
        return matchCount;
    }

    public void setMatchCount(int matchCount) {
        this.matchCount = matchCount;
    }

    public HashMap<String, Map<String, String>> getMap() {
        return map;
    }

//    public void setMap(HashMap<String, Map<String, String>> map) {
//        this.map = map;
//    }

    public MyExpandableListAdapter(Activity activity, ArrayList<FootballMatchBean> footballMatchBeanArrayList) {
        this.activity = activity;
        this.footballMatchBeanArrayList = footballMatchBeanArrayList;
    }

    @Override
    public int getGroupCount() {
        return footballMatchBeanArrayList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return footballMatchBeanArrayList.get(i).getMatchList().size();
    }

    @Override
    public Object getGroup(int i) {
        return footballMatchBeanArrayList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return footballMatchBeanArrayList.get(i).getMatchList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_parent, null);
        } else {

        }
        TextView text = (TextView) view.findViewById(R.id.list_item_parent_title);
        ImageView list_item_parent_icon = view.findViewById(R.id.list_item_parent_icon);
        text.setText(Html.fromHtml(footballMatchBeanArrayList.get(i).getGroupTime() + "  共<font color='red'>" + footballMatchBeanArrayList.get(i).getMatchList().size() + "</font>场比赛"));
        if (b) {
            list_item_parent_icon.setImageResource(R.mipmap.cz);
        } else {
            list_item_parent_icon.setImageResource(R.mipmap.d0);
        }
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ChildViewHolder childViewHolder = null;
        FootballMatchBean.MatchInfo matchInfo = footballMatchBeanArrayList.get(i).getMatchList().get(i1);
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_zq, null);
            childViewHolder = new ChildViewHolder(view);
            view.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) view.getTag();
        }
        childViewHolder.tv_cn_abbr.setText(matchInfo.getL_cn_abbr());
//        childViewHolder.tv_match_end_time.setText(com.itboye.bylibrary.util.TimeUtils.getFormatTime(Long.parseLong(matchInfo.getTime()), "HH:mm") + "截止");
        childViewHolder.tv_match_end_time.setText(matchInfo.getTime()+ "截止");
        childViewHolder.tv_match_number.setText(matchInfo.getNum());
        childViewHolder.tv_match_team_name.setText(matchInfo.getH_cn_abbr() + "      VS      " + matchInfo.getA_cn_abbr());
        childViewHolder.cb_more.setTag(R.id.tag_1, i);
        childViewHolder.cb_more.setTag(R.id.tag_2, i1);
        resetAllStatus(childViewHolder);
        switch (this.pager) {
            case 0:
                //spf 胜平负
                //混合投注
                childViewHolder.ll_hunhe_root.setVisibility(View.VISIBLE);
                addHunTouSpf(matchInfo.getHAD(), childViewHolder, i, i1);
                //rspf 让球胜平负
                addHunTouRspf(matchInfo.getHHAD(), childViewHolder, i, i1);
                setCheckCount(i, i1, childViewHolder);
                break;
            case 1://胜平负
                addSpf(matchInfo.getHAD(), childViewHolder, i, i1, "0_spf_");
                childViewHolder.ll_spf_root.setVisibility(View.VISIBLE);
                break;
            case 2://让球胜平负
                addSpf(matchInfo.getHHAD(), childViewHolder, i, i1, "1_rspf_");
                childViewHolder.ll_spf_root.setVisibility(View.VISIBLE);
                break;
            case 3:
                //总进球
                childViewHolder.ll_zjq_root.setVisibility(View.VISIBLE);
                addZjq(matchInfo.getTTG(), childViewHolder, i, i1);
                break;
            case 4:
                //比分
                addBf(matchInfo.getCRS(), childViewHolder, i, i1);
                childViewHolder.ll_bf_root.setVisibility(View.VISIBLE);
//                addBfDetail(i, i1);
                break;
            case 5://半全场
                childViewHolder.ll_bqc_root.setVisibility(View.VISIBLE);
                addBqc(matchInfo.getHAFU(), childViewHolder, i, i1);
                break;
        }
        childViewHolder.btn_choose_bf.setText(TextUtils.isEmpty(footballMatchBeanArrayList.get(i).getMatchList().get(i1).getScoreDetail()) ? "点击选择比分投注" : footballMatchBeanArrayList.get(i).getMatchList().get(i1).getScoreDetail());
        return view;
    }

    private void addBfDetail(int gP, int cP) {
        Iterator<String> iterator = map.keySet().iterator();
        StringBuilder stringBuilder = new StringBuilder();
        while (iterator.hasNext()) {
            String key = iterator.next();
            if (key.endsWith("3") && key.split("_")[0].equals(gP + "") && key.split("_")[1].equals(cP + "")) {
                Map<String, String> map2 = map.get(key);
                Iterator<String> iterator2 = map2.keySet().iterator();
                while (iterator2.hasNext()) {
                    String key2 = iterator2.next();
                    if (key2.split("_")[6].equals("9:9")) {
                        stringBuilder.append("(平其它) ");
                    } else if (key2.split("_")[6].equals("0:9")) {
                        stringBuilder.append("(负其它) ");
                    } else if (key2.split("_")[6].equals("9:0")) {
                        stringBuilder.append("(胜其它) ");
                    } else {
                        stringBuilder.append("(" + key2.split("_")[6] + ") ");
                    }
                }
            }
        }
        footballMatchBeanArrayList.get(gP).getMatchList().get(cP).setScoreDetail(TextUtils.isEmpty(stringBuilder.toString()) ? "点击选择比分投注" : stringBuilder.toString());
    }

    private void setCheckCount(int i, int i1, ChildViewHolder childViewHolder) {
        Iterator<String> keySet = map.keySet().iterator();
        int count = 0;
        while (keySet.hasNext()) {
            String mapKey = keySet.next();
            String str1 = mapKey.split("_")[0];
            String str2 = mapKey.split("_")[1];
//            String str3=mapKey.split("_")[2];
            if (str1.equals(i + "") && str2.equals(i1 + "")) {
                count += map.get(mapKey).size();
            }
        }
        if (count == 0) {
            childViewHolder.cb_more.setText("更多玩法");
        } else {
            childViewHolder.cb_more.setText("已选中" + count + "项");
        }

    }

    private void addZjq(FootballMatchBean.MatchInfo.TTGBean ttg, ChildViewHolder childViewHolder, int gPosition, int i1) {
        for (int i = 0; i < childViewHolder.ll_zjq_root.getChildCount(); i++) {
            CheckBox checkBox = (CheckBox) childViewHolder.ll_zjq_root.getChildAt(i);
            String str = "";
            checkBox.setOnCheckedChangeListener(null);//必须添加，不然选中状态会丢失
            switch (i) {
                case 0:
                    checkBox.setText(Html.fromHtml(i + "<br><font color='#929292'>" + ttg.getS0() + "</font>"));
                    str = ttg.getS0();
                    break;
                case 1:
                    checkBox.setText(Html.fromHtml(i + "<br><font color='#929292'>" + ttg.getS1() + "</font>"));
                    str = ttg.getS1();
                    break;
                case 2:
                    checkBox.setText(Html.fromHtml(i + "<br><font color='#929292'>" + ttg.getS2() + "</font>"));
                    str = ttg.getS2();
                    break;
                case 3:
                    checkBox.setText(Html.fromHtml(i + "<br><font color='#929292'>" + ttg.getS3() + "</font>"));
                    str = ttg.getS3();
                    break;
                case 4:
                    checkBox.setText(Html.fromHtml(i + "<br><font color='#929292'>" + ttg.getS4() + "</font>"));
                    str = ttg.getS4();
                    break;
                case 5:
                    checkBox.setText(Html.fromHtml(i + "<br><font color='#929292'>" + ttg.getS5() + "</font>"));
                    str = ttg.getS5();
                    break;
                case 6:
                    checkBox.setText(Html.fromHtml(i + "<br><font color='#929292'>" + ttg.getS6() + "</font>"));
                    str = ttg.getS6();
                    break;
                case 7:
                    checkBox.setText(Html.fromHtml(i + "<br><font color='#929292'>" + ttg.getS7() + "</font>"));
                    str = ttg.getS7();
                    break;
            }
            StringBuilder tag = new StringBuilder("2_jqs_" + i + "_" + str + "_" + i + "_" + ttg.getSingle());
            checkBox.setTag(tag);
            Log.v("checkBox", "tag 球胜平负   " + tag.toString());
            checkBox.setChecked(isCheckThisView(tag.toString(), gPosition + "_" + i1 + "_" + "2"));
            setOnClickListener(checkBox, gPosition, i1);
        }
        setIsSingle(childViewHolder.iv_danguan, ttg.getSingle());
    }

    private void addBf(FootballMatchBean.MatchInfo.CRSBean crsBean, ChildViewHolder childViewHolder, int i, int i1) {
        childViewHolder.btn_choose_bf.setOnClickListener(view -> {
            showMorePlayTypes(i, i1, LotteryType.BF, childViewHolder);
        });
        setIsSingle(childViewHolder.iv_danguan, crsBean.getSingle());
    }

    private void addBqc(FootballMatchBean.MatchInfo.HAFUBean hafu, ChildViewHolder childViewHolder, int i, int i1) {
        String str = "";
        for (int i2 = 0; i2 < childViewHolder.ll_bqc_child_1.getChildCount(); i2++) {
            CheckBox checkBox = (CheckBox) childViewHolder.ll_bqc_child_1.getChildAt(i2);
            switch (i2) {
                case 0:
                    str = "胜胜 " + hafu.getSpecial_odds().getHh();
                    break;
                case 1:
                    str = "平胜 " + hafu.getSpecial_odds().getDh();
                    break;
                case 2:
                    str = "负胜 " + hafu.getSpecial_odds().getAh();
                    break;
            }
            checkBox.setText(str);
            setOnClickListener(checkBox, i, i1);
            StringBuilder tag = new StringBuilder("4_bqc_" + i2 + "_" + str.replace(" ", "_") + "_" + hafu.getSingle());
            checkBox.setTag(tag);
            Log.v("checkBox", "tag 球胜平负   " + tag.toString());
            checkBox.setChecked(isCheckThisView(tag.toString(), i + "_" + i1 + "_" + "4"));
        }
        for (int i2 = 0; i2 < childViewHolder.ll_bqc_child_2.getChildCount(); i2++) {
            CheckBox checkBox = (CheckBox) childViewHolder.ll_bqc_child_2.getChildAt(i2);
            switch (i2) {
                case 0:
                    str = "胜平 " + hafu.getSpecial_odds().getHd();
                    break;
                case 1:
                    str = "平平 " + hafu.getSpecial_odds().getDd();
                    break;
                case 2:
                    str = "负平 " + hafu.getSpecial_odds().getAd();
                    break;
            }
            checkBox.setText(str);
            setOnClickListener(checkBox, i, i1);
            StringBuilder tag = new StringBuilder("4_bqc_" + i2 + "_" + str.replace(" ", "_") + "_" + hafu.getSingle());
            checkBox.setTag(tag);
            Log.v("checkBox", "tag 球胜平负   " + tag.toString());
            checkBox.setChecked(isCheckThisView(tag.toString(), i + "_" + i1 + "_" + "4"));
        }
        for (int i2 = 0; i2 < childViewHolder.ll_bqc_child_3.getChildCount(); i2++) {
            CheckBox checkBox = (CheckBox) childViewHolder.ll_bqc_child_3.getChildAt(i2);
            switch (i2) {
                case 0:
                    str = "胜负 " + hafu.getSpecial_odds().getHa();
                    break;
                case 1:
                    str = "平负 " + hafu.getSpecial_odds().getDa();
                    break;
                case 2:
                    str = "负负 " + hafu.getSpecial_odds().getAa();
                    break;
            }
            checkBox.setText(str);
            setOnClickListener(checkBox, i, i1);
            StringBuilder tag = new StringBuilder("4_bqc_" + i2 + "_" + str.replace(" ", "_") + "_" + hafu.getSingle());
            checkBox.setTag(tag);
            Log.v("checkBox", "tag 球胜平负   " + tag.toString());
            checkBox.setChecked(isCheckThisView(tag.toString(), i + "_" + i1 + "_" + "4"));
        }
        setIsSingle(childViewHolder.iv_danguan, hafu.getSingle());
    }

    private void addSpf(FootballMatchBean.MatchInfo.HADBean hadBean, ChildViewHolder childViewHolder, int gPosition, int cPosition, String prifix) {
        for (int i = 0; i < childViewHolder.ll_spf_root.getChildCount(); i++) {
            CheckBox checkBox = (CheckBox) childViewHolder.ll_spf_root.getChildAt(i);
            String str = "";
            checkBox.setOnCheckedChangeListener(null);//必须添加，不然选中状态会丢失
            if (hadBean == null) {
                checkBox.setText("未开售");
                checkBox.setEnabled(false);
            } else {
                checkBox.setEnabled(true);
                switch (i) {
                    case 0:
                        checkBox.setText("主胜 " + hadBean.getH());//胜
                        str = "主胜_" + hadBean.getH();
                        break;
                    case 1:
                        checkBox.setText("平 " + hadBean.getD());//胜
                        str = "平_" + hadBean.getD();
                        break;
                    case 2:
                        checkBox.setText("客胜 " + hadBean.getA());//胜
                        str = "客胜_" + hadBean.getA();
                        break;
                }
                StringBuilder tag = new StringBuilder(prifix + i + "_" + str + "_" + hadBean.getSingle());
                checkBox.setTag(tag);
                Log.v("checkBox", "tag 球胜平负   " + tag.toString());
                checkBox.setChecked(isCheckThisView(tag.toString(), gPosition + "_" + cPosition + "_" + prifix.substring(0, 1)));
                setOnClickListener(checkBox, gPosition, cPosition);
            }
        }
        childViewHolder.tv_spf_txt.setText(hadBean.getFixedodds());
        if (hadBean != null) {
            setIsSingle(childViewHolder.iv_danguan, hadBean.getSingle());
        }
    }


    private void resetAllStatus(ChildViewHolder childViewHolder) {
        childViewHolder.ll_hunhe_root.setVisibility(View.GONE);
        childViewHolder.ll_spf_root.setVisibility(View.GONE);
        childViewHolder.ll_bqc_root.setVisibility(View.GONE);
        childViewHolder.ll_bf_root.setVisibility(View.GONE);
        childViewHolder.ll_zjq_root.setVisibility(View.GONE);
        childViewHolder.iv_danguan.setVisibility(View.GONE);
        childViewHolder.iv_danguan2.setVisibility(View.GONE);
        childViewHolder.cb_more.setText("更多玩法");
        childViewHolder.btn_choose_bf.setText("点击选择比分投注");
        childViewHolder.tv_spf_txt.setText("0");
        childViewHolder.tv_rqspf_txt.setText("0");
    }

    private void addHunTouSpf(FootballMatchBean.MatchInfo.HADBean hadBean, ChildViewHolder childViewHolder, int gPosition, int cPosition) {
        for (int i = 0; i < childViewHolder.ll_spf_odds.getChildCount(); i++) {
            CheckBox checkBox = (CheckBox) childViewHolder.ll_spf_odds.getChildAt(i);
            String str = "";
            checkBox.setOnCheckedChangeListener(null);//必须添加，不然选中状态会丢失
            String tagStr = "";
            if (hadBean == null) {
                checkBox.setText("未开售");
                checkBox.setEnabled(false);
            } else {
                checkBox.setEnabled(true);
                switch (i) {
                    case 0:
                        checkBox.setText("主胜 " + hadBean.getH());//胜
                        tagStr = "主胜_" + hadBean.getH();
                        break;
                    case 1:
                        checkBox.setText("平 " + hadBean.getD());//胜
                        tagStr = "平_" + hadBean.getD();
                        break;
                    case 2:
                        checkBox.setText("客胜 " + hadBean.getA());//胜
                        tagStr = "客胜_" + hadBean.getA();
                        break;
                }
                StringBuilder tag = new StringBuilder("0_spf_" + i + "_" + tagStr + "_" + hadBean.getSingle());
                checkBox.setTag(tag);
                Log.v("checkBox", "tag 球胜平负   " + tag.toString());
                checkBox.setOnClickListener(view -> {
                    reGetData(checkBox, gPosition, cPosition);
                    notifyDataSetChanged();
                });
                checkBox.setChecked(isCheckThisView(tag.toString(), gPosition + "_" + cPosition + "_" + "0"));
                childViewHolder.tv_spf_txt.setText(hadBean.getFixedodds());
                setIsSingle(childViewHolder.iv_danguan2, hadBean.getSingle());
            }
        }
    }

    private void reGetData(CheckBox checkBox, int gPosition, int cPosition) {
        String tags = checkBox.getTag().toString();
        String key = gPosition + "_" + cPosition + "_" + tags.substring(0, 1);
        String value = tags;
        //同一场比赛目前只能限制一种投法
//        if (isSelectCurrentMatch(key)) {
//            ByAlert.alert("一场比赛只能选择一种投注玩法");
//            return;
//        }
        if (isExceedMatchCount(gPosition, cPosition)) {
            return;
        }
        if (map.get(key) != null) {
            //有则删除 无则添加 - 等同于没选中则选中 选中了则取消
            if (map.get(key).get(value) != null) {
                map.get(key).remove(value);
            } else {
                map.get(key).put(value, value);
            }
        } else {
            Map<String, String> mapInner = new HashMap<>();
            mapInner.put(value, value);
            map.put(key, mapInner);
        }
        if (map.get(key) != null && map.get(key).values().size() <= 0)
            map.remove(key);
        caculateTotalSelectMatches();
    }

    private boolean isSelectCurrentMatch(String key) {
        boolean hasSelect = false;
        Iterator<String> keySet = map.keySet().iterator();
        while (keySet.hasNext()) {
            String mapKey = keySet.next();
            String str1 = mapKey.split("_")[0];
            String str2 = mapKey.split("_")[1];
            String str3 = mapKey.split("_")[2];
            if (str1.equals(key.split("_")[0]) && str2.equals(key.split("_")[1])) {
                if (str3.equals(key.split("_")[2])) {

                } else {
                    return true;
                }
            }
        }
//        if (map.get(key) != null) {
//            return true;
////            String betType = key.split("_")[2];//投注玩法
////            map
//        }
        Log.v("", "key---" + key);
        return false;
    }

    private boolean isExceedMatchCount(int gPosition, int cPosition) {
        if (!hashSet.contains(gPosition + "" + cPosition) && hashSet.size() >= 8) {
            ByAlert.alert("最多选择八场比赛");
            return true;
        }
        return false;
    }

    public void caculateTotalSelectMatches() {
        Set<String> setKey = map.keySet();
        Iterator<String> iterator = setKey.iterator();
        // 从while循环中读取key  自己构造投注单相关信息
        zqBetListItemArrayList = new ArrayList<>();
        hashSet = new HashSet<>();
        while (iterator.hasNext()) {
            ZqBetListItem zqBetListItem = new ZqBetListItem();
            String cKey = iterator.next();
            Map<String, String> value = map.get(cKey);
            ArrayList<ZqBetListItem.SpfBean> spfBeanArrayList = new ArrayList<>();
            ArrayList<ZqBetListItem.SpfBean> rspfBeanArrayList = new ArrayList<>();
            ArrayList<ZqBetListItem.SpfBean> bfBeanArrayList = new ArrayList<>();
            ArrayList<ZqBetListItem.SpfBean> zjqBeanArrayList = new ArrayList<>();
            ArrayList<ZqBetListItem.SpfBean> bqcBeanArrayList = new ArrayList<>();
            for (String s : value.keySet()) {
                String args[] = s.split("_");
                int single= Integer.parseInt(args[5]);
//                if (pager==0) {
//                    zqBetListItem.setSingle(single);
//                }
                ZqBetListItem.SpfBean bean = new ZqBetListItem.SpfBean();
                bean.setSingle(single);
                if (args[0].equals("0")) {//胜平负
                    bean.setKey(args[3]);
                    bean.setValue(args[4]);
                    spfBeanArrayList.add(bean);
                } else if (args[0].equals("1")) {//让球胜平负
                    bean.setKey(args[3]);
                    bean.setValue(args[4]);
                    rspfBeanArrayList.add(bean);
                } else if (args[0].equals("2")) {//总进球
                    bean.setKey(args[2] + "球");
                    bean.setValue(args[3]);
                    zjqBeanArrayList.add(bean);
                } else if (args[0].equals("3")) {//比分
                    bean.setKey(args[4]);//比分值
                    bean.setValue(args[3]);//赔率值
                    bfBeanArrayList.add(bean);
                } else if (args[0].equals("4")) {//半全场
                    bean.setKey(args[4]);
                    bean.setValue(args[3]);
                    bqcBeanArrayList.add(bean);
                }
            }
            String[] keys = cKey.split("_");
            int gP = Integer.parseInt(keys[0]);
            int cP = Integer.parseInt(keys[1]);
            hashSet.add(gP + "" + cP);
            zqBetListItem.setKey(gP + "" + cP);
            zqBetListItem.setEndTime(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getBegin_time());
            zqBetListItem.sethTeam(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getH_cn_abbr());
            zqBetListItem.setaTeam(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getA_cn_abbr());
            zqBetListItem.setDate(footballMatchBeanArrayList.get(gP).getGroupTime());
            zqBetListItem.setbDate(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getB_date());
            zqBetListItem.setNum(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getNum_type());
            zqBetListItem.setgPosition(gP);
            zqBetListItem.setcPosition(cP);
//            int single = 0;
//            if (pager!=0) {
//                switch (pager) {
//                    case 1:
//                        single = Integer.parseInt(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getHAD().getSingle());
//                        break;
//                    case 2:
//                        single = Integer.parseInt(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getHHAD().getSingle());
//                        break;
//                    case 3:
//                        single = Integer.parseInt(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getTTG().getSingle());
//                        break;
//                    case 4:
//                        single = Integer.parseInt(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getCRS().getSingle());
//                        break;
//                    case 5:
//                        single = Integer.parseInt(footballMatchBeanArrayList.get(gP).getMatchList().get(cP).getHAFU().getSingle());
//                        break;
//                }
//                zqBetListItem.setSingle(single);
//            }
            boolean hasAdd = false;
            int position = 0;
            for (int i = 0, zqBetListItemArrayListSize = zqBetListItemArrayList.size(); i < zqBetListItemArrayListSize; i++) {
                ZqBetListItem betListItem = zqBetListItemArrayList.get(i);
                if (betListItem.getKey().equals(gP + "" + cP)) {
                    hasAdd = true;
                    position = i;
                    break;
                } else {
                    hasAdd = false;
                }
            }
            if (!hasAdd) {
                zqBetListItem.setSpfList(spfBeanArrayList);
                zqBetListItem.setRspfList(rspfBeanArrayList);
                zqBetListItem.setBfList(bfBeanArrayList);
                zqBetListItem.setZjqList(zjqBeanArrayList);
                zqBetListItem.setBqcList(bqcBeanArrayList);
                zqBetListItemArrayList.add(zqBetListItem);
            } else {
                zqBetListItemArrayList.get(position).getSpfList().addAll(spfBeanArrayList);
                zqBetListItemArrayList.get(position).getRspfList().addAll(rspfBeanArrayList);
                zqBetListItemArrayList.get(position).getBfList().addAll(bfBeanArrayList);
                zqBetListItemArrayList.get(position).getZjqList().addAll(zjqBeanArrayList);
                zqBetListItemArrayList.get(position).getBqcList().addAll(bqcBeanArrayList);
            }
        }
        ((FootballMainActivity) activity).getToolbar_hiti().setText(Html.fromHtml(hashSet.size() <= 0 ? "请选择比赛" : "已选择<font color='red'>" + hashSet.size() + "</font>场比赛"));
        matchCount = hashSet.size();
    }

    /**
     * 解析tag参数
     *
     * @param tag
     * @return
     */

    boolean isCheckThisView(String tag, String index) {
        if (map == null) return false;
        if (map.get(index) != null) {
            return map.get(index).get(tag) != null;
        }
        return false;
    }

    private void addHunTouRspf(FootballMatchBean.MatchInfo.HADBean hhadBean, ChildViewHolder childViewHolder, final int gPosition, final int cPosition) {
        for (int i = 0; i < childViewHolder.ll_rspf_odds.getChildCount(); i++) {
            CheckBox checkBox = (CheckBox) childViewHolder.ll_rspf_odds.getChildAt(i);
            String str = "";
            checkBox.setOnCheckedChangeListener(null);
            if (hhadBean == null) {
                checkBox.setText("未开售");
                checkBox.setEnabled(false);
            } else {
                checkBox.setEnabled(true);
                switch (i) {
                    case 0:
                        str = "主胜_" + hhadBean.getH();
                        checkBox.setText("主胜 " + hhadBean.getH());//胜
                        break;
                    case 1:
                        str = "平_" + hhadBean.getD();
                        checkBox.setText("平 " + hhadBean.getD());//胜
                        break;
                    case 2:
                        str = "客胜_" + hhadBean.getA();
                        checkBox.setText("客胜 " + hhadBean.getA());//胜
                        break;
                }
                StringBuilder tag = new StringBuilder("1_rspf_" + i + "_" + str + "_" + hhadBean.getSingle());
                checkBox.setTag(tag);
                Log.v("checkBox", "tag 让球胜平负   " + tag.toString());
                checkBox.setChecked(isCheckThisView(tag.toString(), gPosition + "_" + cPosition + "_" + "1"));
                setOnClickListener(checkBox, gPosition, cPosition);
                childViewHolder.tv_rqspf_txt.setText(hhadBean.getFixedodds());
                setIsSingle(childViewHolder.iv_danguan, hhadBean.getSingle());
            }
        }

    }

    private void setOnClickListener(CheckBox checkBox, int gPosition, int cPosition) {
        checkBox.setOnClickListener(view -> {
            reGetData(checkBox, gPosition, cPosition);
            notifyDataSetChanged();
        });
    }

    //是否位单关
    private void setIsSingle(View view, String singleValue) {
        if (singleValue == null) {
            view.setVisibility(View.GONE);
            return;
        }
        //是否单关
        if (singleValue.equalsIgnoreCase("0")) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }


    private void showMorePlayTypes(int gPosition, int cPosition, LotteryType lotteryType, ChildViewHolder childViewHolder) {
        JczqHHDialog dialog = new JczqHHDialog(activity, footballMatchBeanArrayList, gPosition, cPosition, map, lotteryType);
        dialog.setSelectListener((matchInfoArrayList, map1) -> {
            if (isExceedMatchCount(gPosition, cPosition)) {
                return;
            }
            map = map1;
            addBfDetail(gPosition, cPosition);
            caculateTotalSelectMatches();
            notifyDataSetChanged();
        });
        dialog.show();
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    public void setPager(int pager) {
        this.pager = pager;
        map = new HashMap<>();
        notifyDataSetChanged();
    }

    public void deleteSelect() {
        if (map == null) return;
        if (map.size() <= 0) return;
        map.clear();
        caculateTotalSelectMatches();
        for (FootballMatchBean footballMatchBean : footballMatchBeanArrayList) {
            for (FootballMatchBean.MatchInfo matchInfo : footballMatchBean.getMatchList()) {
                matchInfo.setScoreDetail("");
            }
        }
        notifyDataSetChanged();
    }

    class ChildViewHolder {
        CheckBox cb_more;
        TextView tv_cn_abbr, tv_match_number, tv_match_end_time, tv_match_team_name;
        TextView tv_spf_txt, tv_rqspf_txt;
        //胜平负
        LinearLayout ll_spf_odds, ll_rspf_odds, ll_hunhe_root, ll_spf_root, ll_bqc_root, ll_bf_root, ll_zjq_root;

        LinearLayout ll_bqc_child_1, ll_bqc_child_2, ll_bqc_child_3;
        TextView btn_choose_bf;
        ImageView iv_danguan, iv_danguan2;

        public ChildViewHolder(View view) {
            cb_more = view.findViewById(R.id.cb_more);
            cb_more.setOnClickListener(view1 -> {
                //更多玩法
                showMorePlayTypes((int) view1.getTag(R.id.tag_1), (int) view1.getTag(R.id.tag_2), LotteryType.SHOWALL, this);
            });
            tv_cn_abbr = view.findViewById(R.id.tv_cn_abbr);
            tv_match_number = view.findViewById(R.id.tv_match_number);
            tv_match_end_time = view.findViewById(R.id.tv_match_end_time);
            tv_match_team_name = view.findViewById(R.id.tv_match_team_name);
            ll_spf_odds = view.findViewById(R.id.ll_spf_odds);
            ll_rspf_odds = view.findViewById(R.id.ll_rspf_odds);
            ll_hunhe_root = view.findViewById(R.id.ll_hunhe_root);
            ll_spf_root = view.findViewById(R.id.ll_spf_root);
            ll_bqc_root = view.findViewById(R.id.ll_bqc_root);
            ll_bf_root = view.findViewById(R.id.ll_bf_root);
            ll_zjq_root = view.findViewById(R.id.ll_zjq_root);
            btn_choose_bf = view.findViewById(R.id.btn_choose_bf);
            ll_bqc_child_1 = view.findViewById(R.id.ll_bqc_child_1);
            ll_bqc_child_2 = view.findViewById(R.id.ll_bqc_child_2);
            ll_bqc_child_3 = view.findViewById(R.id.ll_bqc_child_3);
            iv_danguan = view.findViewById(R.id.iv_danguan);
            iv_danguan2 = view.findViewById(R.id.iv_danguan2);
            tv_spf_txt = view.findViewById(R.id.tv_spf_txt);
            tv_rqspf_txt = view.findViewById(R.id.tv_rqspf_txt);
        }

    }
}
